﻿#include "parametersetting.h"

#include "ui_parametersetting.h"

ParameterSetting::ParameterSetting(QWidget* parent) : QDialog(parent), ui(new Ui::ParameterSetting) {
  ui->setupUi(this);
  QString button_style(
      "QPushButton{border-image: url(:/pic/src/button/key.png);color: white;}"
      "QPushButton:hover{border-image: url(:/pic/src/button/key_hover.png);color: white;}"
      "QPushButton:pressed{border-image: url(:/pic/src/button/key_pressed.png);color: black;}");
  ui->pushButton_ok->setStyleSheet(button_style);
  ui->pushButton_cancel->setStyleSheet(button_style);
  ui->pushButton_apply->setStyleSheet(button_style);
}

ParameterSetting::~ParameterSetting() { delete ui; }

bool ParameterSetting::isOK() { return is_ok; }

void ParameterSetting::on_pushButton_ok_clicked() {
  if (!is_ok && (cur_file != nonefile)) {
    int result = QMessageBox::question(nullptr, "提示", "文件没保存是否关闭?", QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

    if (result == QMessageBox::No) { return; }
  }
  this->close();
}

void ParameterSetting::on_pushButton_cancel_clicked() {
  is_ok = false;
  this->close();
}

void ParameterSetting::on_pushButton_apply_clicked() {
  if (saveFile(cur_file)) { is_ok = true; }
}

bool ParameterSetting::loadFile(const QString& fileName) {
  if (fileName == nonefile) {
    QMessageBox::warning(this, tr("警告"), tr("文件没加载 %1：").arg(fileName));
    return false;
  }
  QFile file(fileName);

  if (!file.open(QFile::ReadWrite | QFile::Text)) {
    QMessageBox::warning(this, tr("警告"), tr("无法读取文件 %1 %2").arg(fileName).arg(file.errorString()));
    return false;
  }
  QTextStream in(&file);

  ui->textEdit->setText(in.readAll());
  return true;
}

bool ParameterSetting::saveFile(const QString& fileName) {
  if (fileName == nonefile) {
    QMessageBox::warning(this, tr("警告"), tr("文件没加载 %1：").arg(fileName));

    return false;
  }
  QFile file(fileName);

  if (!file.open(QFile::WriteOnly | QFile::Text)) {
    QMessageBox::warning(this, tr("警告"), tr("无法写入文件 %1").arg(fileName));
    return false;
  }
  QTextStream out(&file);

  out << ui->textEdit->toPlainText();
  QMessageBox::information(this, tr("提示"), tr("写入文件%1 完成").arg(fileName));
  return true;
}

void ParameterSetting::keyPressEvent(QKeyEvent* e) {
  if ((e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) { on_pushButton_ok_clicked(); }
}
void ParameterSetting::on_pushButton_download_special_clicked() {
  cur_file = "./ini/SpecialParam.ini";
  loadFile(cur_file);
}

void ParameterSetting::on_pushButton_download_agv_clicked() {
  cur_file = "./ini/AgvParam.ini";
  loadFile(cur_file);
}

void ParameterSetting::on_pushButton_download_navi_clicked() {
  cur_file = "./ini/NaviParam.ini";
  loadFile(cur_file);
}

void ParameterSetting::on_pushButton_download_motion_clicked() {
  cur_file = "./ini/MotionParam.ini";
  loadFile(cur_file);
}

void ParameterSetting::on_pushButton_download_com_clicked() {
  cur_file = "./ini/ComParam.ini";
  loadFile(cur_file);
}

void ParameterSetting::on_pushButton_download_qrcod_clicked() {
  cur_file = "./ini/QrcodeParam.ini";
  loadFile(cur_file);
}
void ParameterSetting::on_pushButton_num_0_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("0");
}

void ParameterSetting::on_pushButton_num_1_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("1");
}

void ParameterSetting::on_pushButton_num_2_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("2");
}

void ParameterSetting::on_pushButton_num_3_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("3");
}

void ParameterSetting::on_pushButton_num_4_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("4");
}

void ParameterSetting::on_pushButton_num_5_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("5");
}

void ParameterSetting::on_pushButton_num_6_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("6");
}

void ParameterSetting::on_pushButton_num_7_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("7");
}

void ParameterSetting::on_pushButton_num_8_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("8");
}

void ParameterSetting::on_pushButton_num_9_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("9");
}

void ParameterSetting::on_pushButton_num_13_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText(".");
}

void ParameterSetting::on_pushButton_num_14_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText(",");
}

void ParameterSetting::on_pushButton_num_10_clicked() { ui->textEdit->undo(); }

void ParameterSetting::on_pushButton_num_11_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("-");
}

void ParameterSetting::on_pushButton_num_18_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->insertPlainText("~");
}

void ParameterSetting::on_pushButton_num_15_clicked() {
  ui->textEdit->setFocus();
  QTextCursor cursor = ui->textEdit->textCursor();
  //删除前一个字符
  cursor.deletePreviousChar();
  //设置当前的光标为更改后的光标
  ui->textEdit->setTextCursor(cursor);
}

void ParameterSetting::on_pushButton_num_16_clicked() { ui->textEdit->copy(); }

void ParameterSetting::on_pushButton_num_17_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->paste();
}

void ParameterSetting::on_pushButton_left_move_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->moveCursor(QTextCursor::Left);
}

void ParameterSetting::on_pushButton_right_move_clicked() {
  ui->textEdit->setFocus();
  ui->textEdit->moveCursor(QTextCursor::Right);
}
